<?php
/*
File name: functions.php
Created date: 08/05/2013
Developer: Cuong Phan
*/

//check WP_DEBUG, if set to true, we suppress warning, notice, deprecated, and user notice
//If wpcu3er is activated, this wpcu3er plugin suppress all errors even if WP_DEBUG is set to true, 
//need to set display_errors to 1 in wpcu3er plugin to allow errors for troubleshooting
if(defined('WP_DEBUG') == 1 || WP_DEBUG == true){
	error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED ^ E_USER_NOTICE);
	ini_set('display_errors', '0');
}

define('FOLDER_STR',	get_template()); 				// ~ name template directory
define('HOME_URL',		get_bloginfo('url')); 			//reference http://localhost/your-site
define('STYLESHEET_URL',get_bloginfo('stylesheet_url'));//reference /your-site/wp-content/themes/your-theme/style.css
define('TEMPLATE_URL',	get_bloginfo('template_url'));	//reference /your-site/wp-content/themes/your-theme

define('FOLDER_LIBS', TEMPLATE_URL . '/libs');
define('FOLDER_DATA', TEMPLATE_URL . '/data');
define('FOLDER_IMG', TEMPLATE_URL . '/images');
define('FOLDER_CSS', TEMPLATE_URL . '/css');
define('FOLDER_JS', TEMPLATE_URL . '/js');
define('IS_MINE', true);

function mytheme_setup() {
	// Adds RSS feed links to <head> for posts and comments.
	//add_theme_support( 'automatic-feed-links' );

	// This theme supports a variety of post formats.
	//add_theme_support( 'post-formats', array( 'aside', 'image', 'link', 'quote', 'status' ) );

	// This theme uses wp_nav_menu() in one location(Appearence).
	register_nav_menus( array(
		'header-menu' => 'Header Menu',
		'footer-menu' => 'Footer Menu'
	));
	
	// This theme uses a custom image size for featured images, displayed on "standard" posts.
	add_theme_support( 'post-thumbnails' );
	//set_post_thumbnail_size( 624, 9999 ); // Unlimited height, soft crop
}
add_action( 'after_setup_theme', 'mytheme_setup' );
//Begin mang khong cho nguoi quan tri xoa page or post
$restricted_pages = array (
	'about_us' 		=> 4,
	'contact_us' 	=> 6,
	'gallery' 		=> 8,
	'overview' 		=> 7
);
//End Mang

if ( is_admin() ) {
	
	function highslide_get_image_send_to_editor($html, $id, $caption, $title, $align, $url, $size, $alt) {
		$attachment = get_post($id);
		if ( 'image' === substr( $attachment->post_mime_type, 0, 5 ) ) {
			$image = get_image_tag($id, '', '', $align, $size);
			if ( $url ) {
				$html = '<a class="highslide" onclick="return hs.expand(this)" title="'. (($title) ? $title : $alt) .'" href="'. esc_attr($url) .'">'. $image .'</a>';
			}
		}
		return $html;
	}
	add_filter( 'image_send_to_editor', 'highslide_get_image_send_to_editor', 10, 9 );
	require( 'libs/extra_button_editor.php' ); //Include Full bo go trong Wordpress
	require( 'libs/allow_show_tag_br_p_in_editor.php' ); //Show and Hide tag br and p
	require( 'libs/use_dialog_upload.php' ); //Su dung cho panner slider
	require( 'libs/taxonomy-tinymce.php' ); //Include bo go trong categories
	require( 'libs/custom_settings.php' ); //Dung cho menu dia chi and link mang social 
	require( 'libs/control_admin.php' ); //Insert, Delete, Update cac thuoc tinh trong Wordpress
	require( 'libs/custom_fields_category.php' ); //Insert thuoc tinh trong category
	
} else {
	function custom_wp_title( $title, $sep ) {
		global $paged, $page;
	
		if ( is_feed() )
			return $title;
	
		// Add the site name.
		$title .= get_bloginfo( 'name' );
		
	 	// Add the site description for the home/front page.
		$site_description = get_bloginfo( 'description', 'display' );
		if ( $site_description && ( is_home() || is_front_page() ) )
			$title = "$title $sep $site_description";
	
		// Add a page number if necessary.
		if ( $paged >= 2 || $page >= 2 )
			$title = "$title $sep " . sprintf( __( 'Page %s', 'twentytwelve' ), max( $paged, $page ) );
	
		return $title;
	}
	add_filter( 'wp_title', 'custom_wp_title', 10, 2 );

	$layout		= get_option('_layout_settings');
	//require( 'libs/theme_user_functions.php' );
	function get_category_top_parent_id($catid) {
		while ($catid) {
			$cat = get_category($catid); // get the object for the catid
			$catid = $cat->category_parent; // assign parent ID (if exists) to $catid
			// the while loop will continue whilst there is a $catid
			// when there is no longer a parent $catid will be NULL so we can assign our $catParent
			$catParent = $cat->cat_ID;
		}
		return $catParent;
	}
	// Ham cho phep su dung dung noi dung thuoc tinh doan text
	function get_content_using_wp_editor($the_content) {
		return str_replace(']]>', ']]&gt;', apply_filters('the_content', $the_content));
	}
	// Ham gioi han so ki tu cua the contend and descripton category
	function GetWords($Content, $nLen){
		$strCount = strlen(strip_tags($Content));
		$space = " ";
		if($Content == "" || $Content == null){
			$pos = strpos($Content, $space, 0);
		}elseif($strCount < $nLen){
			$nLen = $strCount - 1;
			$pos = strpos($Content, $space, $nLen);
		}else{
			$pos = strpos($Content, $space, $nLen);
		}
		$result = "";
		if ($pos === false) { // not found --> return original string
			$result = $Content;
		} else { // found --> return some words based on $nLen
			$result = trim(substr($Content,0,$pos)).' ... ';
		}
		return $result;
	}
	//Ham phan trang
	function generate_pagination_ajax($js_function, $div_id, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
	{
		$total_pages = ceil($num_items/$per_page);
		if ( $total_pages == 1 )
			return '';
		$on_page = floor($start_item / $per_page) + 1;
		$page_string = '';
		if ( $total_pages > 10 ) {
			$init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;
			for($i = 1; $i < $init_page_max + 1; $i++) {
				$page_string .= ( $i == $on_page ) ? '<span class="current">' . $i . '</span>' : '<a href="javascript:void(0)" onclick="'. $js_function .'(\''. $div_id .'\','. $per_page .','. ( ( $i - 1 ) * $per_page ) . ')" class="page">' . $i . '</a>';
				if ( $i <  $init_page_max )
					$page_string .= '&nbsp;';
			}
			if ( $total_pages > 3 ) {
				if ( $on_page > 1  && $on_page < $total_pages ) {
					$page_string .= ( $on_page > 5 ) ? ' ... ' : '';
					$init_page_min = ( $on_page > 4 ) ? $on_page : 5;
					$init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;
					for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++) {
						$page_string .= ($i == $on_page) ? '<span class="current">' . $i . '</span>' : '<a href="javascript:void(0)" onclick="'. $js_function .'(\''. $div_id .'\','. $per_page .','. ( ( $i - 1 ) * $per_page ) . ')" class="page">' . $i . '</a>';
						if ( $i <  $init_page_max + 1 )
							$page_string .= '&nbsp;';
					}
					$page_string .= ( $on_page < $total_pages - 4 ) ? ' ... ' : '';
				}
				else
					$page_string .= ' ... ';
	
				for($i = $total_pages - 2; $i < $total_pages + 1; $i++) {
					$page_string .= ( $i == $on_page ) ? '<span class="current">' . $i . '</span>'  : '<a href="javascript:void(0)" onclick="'. $js_function .'(\''. $div_id .'\','. $per_page .','. ( ( $i - 1 ) * $per_page ) . ')" class="page">' . $i . '</a>';
					if( $i <  $total_pages )
						$page_string .= '&nbsp;';
				}
			}
		} else {
			for($i = 1; $i < $total_pages + 1; $i++) {
				$page_string .= ( $i == $on_page ) ? '<span class="current">' . $i . '</span>' : '<a href="javascript:void(0)" onclick="'. $js_function .'(\''. $div_id .'\','. $per_page .','. ( ( $i - 1 ) * $per_page ) . ')" class="page">' . $i . '</a>';
				if ( $i <  $total_pages )
					$page_string .= '&nbsp;';
			}
		}
		if ( $add_prevnext_text ) {
			if ( $on_page > 1 )
				$page_string = '<a href="javascript:void(0)" onclick="'. $js_function .'(\''. $div_id .'\','. $per_page .','. ( ( $on_page - 2 ) * $per_page ) . ')" class="page">&lt;</a>&nbsp;' . $page_string;
			if ( $on_page < $total_pages )
				$page_string .= '&nbsp;<a href="javascript:void(0)" onclick="'. $js_function .'(\''. $div_id .'\','. $per_page .','. ( $on_page * $per_page ) . ')" class="page">&gt;</a>';
		}
		//$page_string = '<div class="tnk_pagenavi">'. $page_string .'</div>';
		return $page_string;
	}
	// Ham lien ket trang
	function qt_custom_breadcrumbs($showOnHome = 0, $showCurrent = 1) 
	{
		//$showOnHome	= 0; 			// 1 - show breadcrumbs on the homepage, 0 - don't show
		$delimiter		= '&raquo;'; 	// delimiter between crumbs
		$home			= 'Home'; 		// text for the 'Home' link
		//$showCurrent	= 1; 			// 1 - show current post/page title in breadcrumbs, 0 - don't show
		$before			= '<span class="current">'; // tag before the current crumb
		$after			= '</span>'; 	// tag after the current crumb
	
		global $post;
		$homeLink = get_bloginfo('url');
		$tax = 'cruise_category';
		if (is_home() || is_front_page()) {
			if ($showOnHome == 1) 
				echo '<div id="crumbs"><a href="' . $homeLink . '">' . $home . '</a></div>';
		} else {
			echo '<div id="crumbs"><a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';
			if ( is_category() ) {
				$thisCat = get_category(get_query_var('cat'), false);
				if ($thisCat->parent != 0) 
					echo get_category_parents($thisCat->parent, TRUE, ' ' . $delimiter . ' ');
				echo $before . single_cat_title('', false) . $after;
			} elseif ( is_single() && !is_attachment() ) {
				if ( get_post_type() != 'post' ) {
					$term = wp_get_post_terms($post->ID, $tax);
					//$post_type = get_post_type_object(get_post_type());
					//$slug = $post_type->rewrite;
					//echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>';
					//if ($showCurrent == 1) 
						//echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
					echo '<a href="' . get_term_link($term[0]->slug, $tax) . '/">' . $term[0]->name . '</a>';
					if ($showCurrent == 1) 
						echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
				} else {
					$cat = get_the_category(); 
					$cat = $cat[0];
					$cats = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
					if ($showCurrent == 0) 
						$cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
					echo $cats;
					if ($showCurrent == 1) 
						echo $before . get_the_title() . $after;
				}
			} elseif ( is_page() && !$post->post_parent ) {
				if ($showCurrent == 1) 
					echo $before . get_the_title() . $after;
			} elseif ( is_page() && $post->post_parent ) {
				$parent_id  = $post->post_parent;
				$breadcrumbs = array();
				while ($parent_id) {
					$page = get_page($parent_id);
					$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
					$parent_id  = $page->post_parent;
				}
				$breadcrumbs = array_reverse($breadcrumbs);
				for ($i = 0; $i < count($breadcrumbs); $i++) {
					echo $breadcrumbs[$i];
					if ($i != count($breadcrumbs)-1) 
						echo ' ' . $delimiter . ' ';
				}
				if ($showCurrent == 1) 
					echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
			} elseif ( is_404() ) {
				echo $before . 'Error 404' . $after;
			}elseif ( is_taxonomy($tax) ) { //Check if a Tag archive page is being displayed.
				$term = get_term_by('slug', get_query_var('term'), $tax);
				//print_r($term);
				echo $before .'<a href="'. get_term_link($term->slug, $tax) .'">'. $term->name .'</a>'. $after;
			}
		
			if ( get_query_var('paged') ) {
				if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) 
					echo ' (';
				echo __('Page') . ' ' . get_query_var('paged');
				if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) 
					echo ')';
			}
			echo '</div>';
		}
	} // end qt_custom_breadcrumbs()
}

//- Include my custom post type if exist -//
require_once(TEMPLATEPATH . '/libs/tours_custom_post_type.php');
require_once(TEMPLATEPATH . '/libs/theme_function.php');
//require( 'libs/shortcode_youtube.php' );
?>